package main;

import java.awt.Graphics;
import java.awt.Image;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 *
 * @author Patrick Bauer
 * @author Adrian Cieluch
 */
public class XMLUebung1 extends JFrame {
    private final int WIDTH = 500;
    private final int HEIGHT = 400;
    public static int n = 0;
    
    JPanel paintArea;
 
    public XMLUebung1() {
        LocationManager.getInstance().create(n);
        LocationManager.getInstance().findBestAndWorst();
        
        this.initUI();
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        for (String s : args) {
          int argument;
          
          try {
              argument = Integer.parseInt(s);
          } catch (NumberFormatException e) {
              argument = 0;
          }
          
          if(argument >= 3 && argument <= 100) {
              XMLUebung1.n = argument;
              break;
          }
        }
        
        if(XMLUebung1.n == 0){
            System.out.println("Please specify a number between 3 and 100 as first argument.");
            System.exit(1);
        }
        
        XMLUebung1 xml = new XMLUebung1();
    }
    
    public final void initUI() {
        JPanel panel = new JPanel();
        this.getContentPane().add(panel);
        panel.setLayout(null);
        
        paintArea = new JPanel() {
            @Override
            public void paint(Graphics g) {
                super.paint(g);
                LocationManager.getInstance().paint(g);
            } 
        };
        
        paintArea.setAutoscrolls(false);
        paintArea.setBounds(10, 10, 460, 280);
        paintArea.setBorder(BorderFactory.createLoweredBevelBorder());
        
        JLabel labelBest = new JLabel();
        labelBest.setText("Bester Standort:");
        labelBest.setLocation(20, 320);
        labelBest.setSize(100, 9);
        labelBest.setVisible(true);
        
        JLabel labelBestResult = new JLabel();
        labelBestResult.setText("Ort " + LocationManager.getInstance().getBest().getID());
        labelBestResult.setToolTipText("Summe der Entfernung: " + (int)LocationManager.getInstance().getBestDistance() + " Pixel");
        labelBestResult.setLocation(250, 320);
        labelBestResult.setSize(100, 9);
        labelBestResult.setVisible(true);
        
        JLabel labelWorst = new JLabel();
        labelWorst.setText("Schlechtester Standort:");
        labelWorst.setLocation(19, 340);
        labelWorst.setSize(150, 9);
        labelWorst.setVisible(true);
        
        JLabel labelWorstResult = new JLabel();
        labelWorstResult.setText("Ort " + LocationManager.getInstance().getWorst().getID());
        labelWorstResult.setToolTipText("Summe der Entfernung: " + (int)LocationManager.getInstance().getWorstDistance() + " Pixel");
        labelWorstResult.setLocation(250, 340);
        labelWorstResult.setSize(100, 9);
        labelWorstResult.setVisible(true);
        
        panel.add(labelBest);
        panel.add(labelBestResult);
        panel.add(labelWorst);
        panel.add(labelWorstResult);
        panel.add(paintArea);
        
        this.setTitle("Uebung 0");
        this.setSize(WIDTH, HEIGHT);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        
        ArrayList<Image> icons = new ArrayList<Image>();
        icons.add(new ImageIcon(getClass().getResource("icons/icon.png")).getImage());
        icons.add(new ImageIcon(getClass().getResource("icons/icon2.png")).getImage());
        
        this.setIconImages(icons);
        this.setVisible(true);
    }

}
